//新入病人，加底色
function new_in() {
    let td_text_elements = document.querySelectorAll('tr td:nth-child(9)')
    for (let i = 0; i < td_text_elements.length; i++) {
        if (td_text_elements[i].innerText === '新入') {
            $(td_text_elements[i]).addClass('back2')
        }
    }
}

// 数据变表格
function table(datas) {
    let table1 = '<table><tr>'
    for (let i = 0; i < datas[0].length; i++) {
        table1 += `<th>${datas[0][i]}</th>`
    }
    table1 += '</tr>'
    for (let i = 0; i < datas[1].length; i++) {
        table1 += '<tr>'
        for (let j = 0; j < datas[1][i].length; j++) {
            if (datas[1][i][j] === 'None' || datas[1][i][j] == 0) {
                table1 += `<td></td>`
            } else {
                table1 += `<td>${datas[1][i][j]}</td>`
            }
        }
        table1 += '</tr>'
    }
    table1 += '</table>'
    return table1
}

// 数据变表格2
function table2(datas) {
    let table1 = '<table><tr></tr>'    
    for (let i = 0; i < datas[1].length; i++) {
        table1 += '<tr>'
        for (let j = 0; j < datas[1][i].length; j++) {
            if (datas[1][i][j] === 'None' || datas[1][i][j] == 0) {
                table1 += `<td></td>`
            } else {
                table1 += `<td>${datas[1][i][j]}</td>`
            }
        }
        table1 += '</tr>'
    }
    table1 += '</table>'
    return table1
}

// 带选择框的表格
function checkbox_table(datas, index = 0) {
    let rows = []
    rows.push('<table>')
    let row_head = ['<tr><th>选择</th>']
    for (let i = 0; i < datas[0].length; i++) {
        row_head.push(`<th>${datas[0][i]}</th>`)
    }
    row_head.push('</tr>')
    rows.push(row_head.join(''))
    for (let i = 0; i < datas[1].length; i++) {
        let row_body = [`<tr><td><input type="checkbox" name="${datas[0][index]}" value="${datas[1][i][index]}"></td>`]
        for (let j = 0; j < datas[1][i].length; j++) {
            if (datas[1][i][j] === 'None' || datas[1][i][j] == 0) { row_body.push(`<td></td>`) } else { row_body.push(`<td>${datas[1][i][j]}</td>`) }

        }
        row_body.push('</tr>')
        rows.push(row_body.join(''))
    }
    rows.push('</table>')
    return rows.join('')

}

function form_table(datas, index = 0) {
    let rows = []
    rows.push(`<form action='' method='get' >`)
    rows.push(checkbox_table(datas, index = index))
    rows.push(`</form>`)
    return rows.join('')
}

//  带圆的字，el是指jquery选择器
function circle_font(el, str1) {
    for (let i = 0; i < str1.length; i++) {
        el.append(`<div>${str1[i]}</div>`)
    }
}

//显示DOM sql是mysql语名  element元素或选择器，fn是函数
function showDom(sql, element, fn) {
    $.post('/sql/', { sql: sql }, function (data, status) {
        $(element).empty().append(fn(data))
        new_in()
    })
}

// 医学公式
function blood_glucose() {
    let bg = document.querySelector('[name=bg]')
    let bw = document.querySelector('[name=bw]')
    let result = document.querySelector('[name=result]')
    result.value = (+bg.value - 5.6) * 0.6 * (+bw.value) / 11.1
    result.value = Math.floor(result.value * 100) / 100
}

/**
 * 根据form表单的id获取表单下所有可提交的表单数据，封装成数组对象
 */
function getFormData(formId){
    var data = {};
    var results = $(formId).serializeArray();
    $.each(results,function(index,item){
        //文本表单的值不为空才处理
        if(item.value && $.trim(item.value)!=""){
            if(!data[item.name]){
                data[item.name]=item.value;
            }else{
                //name属性相同的表单，值以英文,拼接
                data[item.name]=data[item.name]+','+item.value;
            }
        }
    });
    //console.log(data);
    return data;
}

function getNowTime() {
    var now = new Date();
    var year = now.getFullYear(); //得到年份
    var month = now.getMonth(); //得到月份
    var date = now.getDate(); //得到日期
    // var hour =" 00:00:00"; //默认时分秒 如果传给后台的格式为年月日时分秒，就需要加这个，如若不需要，此行可忽略
    month = month + 1;
    month = month.toString().padStart(2, "0");
    date = date.toString().padStart(2, "0");
    var defaultDate = `${year}-${month}-${date}`;
    // console.log(defaultDate)
    return defaultDate
}

function getToday() {
    let now = new Date();
    let year = now.getFullYear(); //得到年份
    let month = now.getMonth(); //得到月份
    let date = now.getDate(); //得到日期
    // let hour =" 00:00:00"; //默认时分秒 如果传给后台的格式为年月日时分秒，就需要加这个，如若不需要，此行可忽略
    month = month + 1;
    month = month.toString().padStart(2, "0");
    date = date.toString().padStart(2, "0");
    let defaultDate = `${year}-${month}-${date}`;
    // console.log(defaultDate)
    return defaultDate
}

function getTodayDate() {
    const today = new Date(); // 获取当前日期和时间
    const year = today.getFullYear(); // 获取年份
    const simpleYear = String(year).slice(-2)  //简单年份
    const month = String(today.getMonth() + 1).padStart(2, '0'); // 获取月份，+1 是因为月份从 0 开始，padStart 用于补零
    const day = String(today.getDate()).padStart(2, '0'); // 获取日期，padStart 用于补零

    return `${simpleYear}${month}${day}`; // 返回格式化后的日期字符串
}

// console.log(getTodayDate()); // 输出格式为 YYMMDD
function formatTime() {
    const now = new Date();
    // const year = now.getFullYear();
    // const month = String(now.getMonth() + 1).padStart(2, '0');
    // const day = String(now.getDate()).padStart(2, '0');
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    
    return `${hours}:${minutes}:${seconds}`;
}

// console.log(formatTime()); 
// 示例输出: "2023-05-15 14:30:45"